package Document;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.xhtmlrenderer.pdf.ITextRenderer;
@SuppressWarnings("deprecation")
public class GeneratePDF {
    
	public void createPdf(String filename, String content, DAO connector, String style)  throws ClassNotFoundException, SQLException, FileNotFoundException 
    {
		String requete = " SELECT style FROM template WHERE LB_TEMPLATE = '?'";
		String css = "" ;
		OutputStream os = new FileOutputStream(filename);	
		requete = requete.replace("?", style);
		Connection connexion=connector.getConnexion();
		PreparedStatement statement=connexion.prepareStatement(requete);
		ResultSet rs = statement.executeQuery(requete);
		rs.next();
		css = rs.getString("style");
		FileWriter file;
    	String Path = "D:/page.html";
    	try 
    	{	
				file = new FileWriter(Path);
				file.write(css+content+"</body></html>");
				file.close();    	
			    ITextRenderer renderer = new ITextRenderer();            
		        renderer.setDocument(new File(Path));
		        renderer.layout();
		        try
		        {
					renderer.createPDF(os);
				}
		        catch (com.lowagie.text.DocumentException e)
		        {
					e.printStackTrace();
				}
		        os.close();	
    	}
    	catch (FileNotFoundException e)
    	{
    		e.printStackTrace();
    	}
    	catch (IOException e)
    	{
    		e.printStackTrace();
    	}    
           
     }
}
